﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Web.Context.Analysis.Entities;

namespace Web.Context.Analysis
{
    public partial class AnalysisSetting : Form
    {
        protected int m_Amount;
        protected StringBuilder m_Msg = new StringBuilder();
        protected SettingHelper m_SettingAcc;
        protected WebAccessHelper m_WebAcc = new WebAccessHelper();

        public AnalysisSetting()
        {
            InitializeComponent();
        }

        private void LoadRowNode(ContextNode parent)
        {
            foreach (RowNode rowNode in m_SettingAcc.m_RowNodes)
            {
                if (rowNode.Parent == parent)
                {
                    List<HtmlElement> hes = m_SettingAcc.GetElements(parent.HtmlContext, rowNode);
                    foreach (HtmlElement he in hes)
                    {
                        rowNode.HtmlContext = he;
                        LoadItmeNode(rowNode);
                    }
                }
            }
        }

        private void LoadItmeNode(RowNode parent)
        {
            bool isBreak = false;
            foreach (ItemNode itemNode in m_SettingAcc.m_ItemNodes)
            {
                if (itemNode.Parent == parent)
                {
                    List<HtmlElement> hes = m_SettingAcc.GetElements(parent.HtmlContext, itemNode);
                    string name = itemNode.Name;

                    foreach (HtmlElement he in hes)
                    {
                        string val = he.GetAttribute(itemNode.ValProperty);
                        bool isHasKeyInfo = m_SettingAcc.IsKeyInfo(val, itemNode.Key);
                        isBreak = !isHasKeyInfo;
                        if (isHasKeyInfo)
                            m_Msg.Append(name + "[" + val + "];");
                    }
                }
                if (isBreak) break;
            }
            if (!isBreak)
            {
                m_Msg.Append("\r\n");
                m_Amount++;
            }
        }

        private void LoadContextNode(UrlNode parent)
        {
            foreach (ContextNode contextNode in m_SettingAcc.m_ContextNodes)
            {
                if (contextNode.Parent == parent)
                {
                    List<HtmlElement> hes = m_SettingAcc.GetElements(parent.Document, contextNode);
                    foreach (HtmlElement he in hes)
                    {
                        contextNode.HtmlContext = he;
                        LoadRowNode(contextNode);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (m_SettingAcc == null) m_SettingAcc = new SettingHelper();
            m_Msg = new StringBuilder();
            m_Amount = 0;
            foreach (UrlNode urlNode in m_SettingAcc.m_UrlNodes)
            {
                urlNode.Document = m_WebAcc.GetHtmlDocmentByUrl(urlNode.Value, true, wb);
                LoadContextNode(urlNode);
            }
            new SendMail("mantislai@139.com", "mantislai@139.com", m_Msg.ToString(), "51buyAmount[" + m_Amount + "]",
                         "ydabc123").Send();
            MessageBox.Show(m_Msg.ToString());
        }

        private void AnalysisSetting_Load(object sender, EventArgs e)
        {
        }
    }
}